<template>
  <div>
    <!-- data对应数据 -->
    <!-- row-key 选择的依据 通过id选择 -->
    <!-- tree-props 属性数据的属性 -->
    <!-- default-expand-all 默认展开所有子集 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <!-- sortable 排序 -->
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="specsname" label="属性名称" width="180">
      </el-table-column>
      <el-table-column prop="attrs" label="属性值" width="180">
        <template slot-scope="scope">
          <div>
            <el-tag size="mini" type="success" v-for="item in scope.row.attrs" :key="item">{{item}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <div>
            <el-tag type="success" v-if="scope.row.status == 1">已启用</el-tag>
            <el-tag type="danger" v-if="scope.row.status == 2">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(scope.row)"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="del(scope.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- current-change 修改页码，跳转页面 -->
    <!-- current-page 页码数 -->
    <!-- page-size 每页显示几条 -->
    <!-- layout 要显示的控件 -->
    <!-- total 总条数 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="size"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { spaceList, getSpecsCount,specsDel } from "@/api.js";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1, //页码数
      size: 1, //每页几个
      total: 1, //一共几个
    };
  },
  created() {
    this.getCount();
  },
  methods: {
    //修改页码数，获取另一页的数据
    handleCurrentChange(val) {
      this.currentPage=val
      this.getSpecs();
    },
    async getSpecs() {
      let res = await spaceList({
        size: this.size,
        page: this.currentPage,
      });
      this.tableData = res.list;
    },
    async getCount(){
      let res2 = await getSpecsCount();
      this.total = res2.list[0].total;
      this.getSpecs()
    },
    edit(info) {
      this.$emit("edit", info);
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await specsDel(id);
          if (res.code == 200) {
            this.currentPage=1
           this.getCount()
          }
          console.log(res);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style scoped>
</style>